package com.wcx.application.controller;

import com.wcx.application.config.MyCustomAnnotation;
import com.wcx.application.domian.BossBean;
import com.wcx.application.domian.StudentBean;
import com.wcx.application.service.BossService;
import com.wcx.application.service.StudentService;
import com.wcx.application.utils.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

@RestController
@RequestMapping("/boss")
@CrossOrigin
public class BossController {

    @Autowired
    BossService bossService;
    @Value("${web.upload-path}")
    private String uploadPath;


    @PostMapping("/login")
    public Result login(@RequestBody BossBean bossBean) {
        BossBean login = bossService.login(bossBean);

        if (login != null) {
            return Result.ok().data("data", login);
        } else {
            return Result.error();
        }
    }

    @GetMapping("/list")
    public Result getList() {
        System.out.println("---" + bossService.getList());
        return Result.ok().data("list", bossService.getList());
    }

    @PostMapping("/update")
    public Result update(@RequestBody BossBean bossBean) {
        int i = bossService.update(bossBean);
        if (i != 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @PostMapping("/search")
    public Result search(@RequestBody BossBean bossBean) {
        System.out.println("---123"+bossBean.toString());
        return Result.ok().data("list", bossService.searchList(bossBean));
    }

    @PostMapping("/create")
    public Result create(@RequestBody BossBean bossBean) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY年MM月dd日 HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        String time = simpleDateFormat.format(calendar.getTime());
        bossBean.setCreateTime(time);
        if (bossBean.getAvatar() == ""|| bossBean.getAvatar()==null) {
            bossBean.setAvatar("fe648697a381d920e8324efbcdbc032a.jpg");
        }
        int i = bossService.create(bossBean);
        if (i != 0) {
            return Result.ok();
        } else {
            return Result.error();
        }

    }

    @DeleteMapping("/delete")
    public Result delete(int id) {
        int i = bossService.delete(id);
        if (i != 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile photo, @RequestParam("id") int id, HttpServletRequest request) {
        System.out.println(photo.getOriginalFilename());
        System.out.println(photo.getContentType());

        int i = bossService.updateAvatarById(photo.getOriginalFilename(), id);
        saveFile(photo, uploadPath);
        if (i != 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @PostMapping("/uploadA")
    public Result uploadA(@RequestParam("file") MultipartFile photo, String username, HttpServletRequest request) {
        System.out.println(photo.getOriginalFilename());
        System.out.println(photo.getContentType());

        int i = bossService.updateAvatarByUserName(photo.getOriginalFilename(), username);
        saveFile(photo, uploadPath);
        if (i != 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    private void saveFile(MultipartFile photo, String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdir();
        }
        File file1 = new File(path + photo.getOriginalFilename());
        try {
            photo.transferTo(file1);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}